home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / combination / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-10  |  2.6 KB  |  123 lines

  1. /* combination/init.c
  2.  * based on permutation/init.c by Brian Gough
  3.  * 
  4.  * Copyright (C) 2001 Szymon Jaroszewicz
  5.  * 
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or (at
  9.  * your option) any later version.
  10.  * 
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include <gsl/gsl_errno.h>
  24. #include <gsl/gsl_combination.h>
  25.  
  26. gsl_combination *
  27. gsl_combination_alloc (const size_t n, const size_t k)
  28. {
  29.   gsl_combination * c;
  30.  
  31.   if (n == 0)
  32.     {
  33.       GSL_ERROR_VAL ("combination parameter n must be positive integer",
  34.             GSL_EDOM, 0);
  35.     }
  36.   if (k > n)
  37.     {
  38.       GSL_ERROR_VAL ("combination length k must be an integer less than or equal to n",
  39.             GSL_EDOM, 0);
  40.     }
  41.   c = (gsl_combination *) malloc (sizeof (gsl_combination));
  42.  
  43.   if (c == 0)
  44.     {
  45.       GSL_ERROR_VAL ("failed to allocate space for combination struct",
  46.             GSL_ENOMEM, 0);
  47.     }
  48.  
  49.   if (k > 0)
  50.     {
  51.       c->data = (size_t *) malloc (k * sizeof (size_t));
  52.  
  53.       if (c->data == 0)
  54.         {
  55.       free (c);        /* exception in constructor, avoid memory leak */
  56.  
  57.       GSL_ERROR_VAL ("failed to allocate space for combination data",
  58.              GSL_ENOMEM, 0);
  59.         }
  60.     }
  61.  
  62.   c->n = n;
  63.   c->k = k;
  64.  
  65.   return c;
  66. }
  67.  
  68. gsl_combination *
  69. gsl_combination_calloc (const size_t n, const size_t k)
  70. {
  71.   size_t i;
  72.  
  73.   gsl_combination * c =  gsl_combination_alloc (n, k);
  74.  
  75.   if (c == 0)
  76.     return 0;
  77.  
  78.   /* initialize combination to identity */
  79.  
  80.   for (i = 0; i < k; i++)
  81.     {
  82.       c->data[i] = i;
  83.     }
  84.  
  85.   return c;
  86. }
  87.  
  88. void
  89. gsl_combination_init_first (gsl_combination * c)
  90. {
  91.   const size_t k = c->k ;
  92.   size_t i;
  93.  
  94.   /* initialize combination to identity */
  95.  
  96.   for (i = 0; i < k; i++)
  97.     {
  98.       c->data[i] = i;
  99.     }
  100. }
  101.  
  102. void
  103. gsl_combination_init_last (gsl_combination * c)
  104. {
  105.   const size_t k = c->k ;
  106.   size_t i;
  107.   size_t n = c->n;
  108.  
  109.   /* initialize combination to identity */
  110.  
  111.   for (i = 0; i < k; i++)
  112.     {
  113.       c->data[i] = n - k + i;
  114.     }
  115. }
  116.  
  117. void
  118. gsl_combination_free (gsl_combination * c)
  119. {
  120.   if (c->k > 0) free (c->data);
  121.   free (c);
  122. }
  123.